package com.twl.app.base.ctrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.twl.app.base.bo.CommonBoImpl;
import com.twl.app.base.entity.CheckDate;
import com.twl.app.base.entity.Checktemplet;
import com.twl.app.base.entity.Jbsalary;
import com.twl.app.base.entity.LockState;
import com.twl.app.base.entity.OpenState;
import com.twl.app.base.entity.SalSalaryData;
import com.twl.app.base.entity.Uemp;
import com.twl.app.base.util.BigdecUitl;
import com.twl.app.base.util.CryptUtils;
import com.twl.app.base.util.DateComm;
import com.twl.core.framework.ctrl.AbsCrudCtrl;

@Service("commonCtrlImpl")
public class CommonCtrlImpl extends AbsCrudCtrl<CommonBoImpl> {

	@Autowired
	private DateComm dateComm;

	
	/**
	 * 将查出的uemp list 集合转map key为staffint value为uemp
	 * @return
	 */
	public Map<String, Uemp> listToMapForUemp(){
		List<Uemp> list = bo.getUemps();
		Map<String, Uemp> map = new HashMap<String, Uemp>();
		for (Uemp uemp : list) {
			map.put(uemp.getStaffint(), uemp);
		}
		if(map!=null){
			return map;
		}
		return null ;
	}
	/**
	 * 获取整个月的考勤信息（yyyyMM）
	 * 
	 * @param date
	 * @return
	 */
	public List<Checktemplet> getChecktempletList(String date) {

		return bo.getChecktempletListByDate(date);
	}

	/**
	 * 获取模板信息
	 * 
	 * @param staffint
	 * @return
	 */
	public Checktemplet getChecktemplet(String staffint) {

		return bo.getChecktemplet(staffint);
	}

	/***
	 * 查询薪资表信息
	 * 
	 * @param id
	 * @return
	 */
	public SalSalaryData getsalsalarydate(String id,String data) {
		return bo.getsalsalarydate(id,data);
	}

	/**
	 * 获取用户信息模块
	 * 
	 * @param staffint
	 * @return
	 */
	public Uemp getUemp(String staffint) {

		return bo.getUemp(staffint);
	}

	/**
	 * 查询基本工资
	 * 
	 * @param id
	 * @return
	 */
	public Jbsalary findjbsalary(String id) {
		return bo.findjbsalary(id);
	}

	/***
	 * 查询以前的工资
	 * 
	 * @param id
	 * @return
	 */
	public Jbsalary findjbsalarying(String id) {
		return bo.findjbsalarying(id);
	}

	/**
	 * 得到应出勤天数(上月)考勤表导入的日期
	 * 
	 * @param yearmouth
	 * @return
	 */
	public Integer getOutforworkint(String yearmouth) {
		return bo.getOutforworkint(yearmouth);

	}

	/**
	 * 得到应出勤天数(上月)日期这个日期不要加入节假日xxj
	 * 
	 * @param yearmouth
	 * @return
	 */
	public Integer getOutwradint(String yeamonth) {
		return bo.getNewmoth(yeamonth);
	}

	/**
	 * 得到应出勤天数(本月)导入考勤的月份
	 * 
	 * @param yearmouth
	 * @return
	 */
	public Integer getOutforworkintNow(String yearmouth) {
		String month = yearmouth.substring(4);
		String year = yearmouth.substring(0, 4);
		if ("12".equals(month)) {
			String newYear = String.valueOf((Integer.valueOf(year) + 1));
			String newYearMonth = newYear + "01";
			return bo.getOutforworkint(newYearMonth);
		} else {
			/*
			 * String newMonth = String.valueOf((Integer.valueOf(month) + 1));
			 * String newYearMonth = year + newMonth;
			 */
			String newYearMonth = String
					.valueOf(Integer.valueOf(yearmouth) + 1);
			return bo.getOutforworkint(newYearMonth);
		}
	}
	/**
	 * 得到应出勤天数(本月)导入考勤的月份xxj
	 * 
	 * @param yearmouth
	 * @return
	 */
	public Integer getNewmonth(String yearmouth) {
		String month = yearmouth.substring(4);
		String year = yearmouth.substring(0, 4);
		if ("12".equals(month)) {
			String newYear = String.valueOf((Integer.valueOf(year) + 1));
			String newYearMonth = newYear + "01";
			return bo.getNewmoth(newYearMonth);
		} else {
			/*
			 * String newMonth = String.valueOf((Integer.valueOf(month) + 1));
			 * String newYearMonth = year + newMonth;
			 */
			String newYearMonth = String
					.valueOf(Integer.valueOf(yearmouth) + 1);
			return bo.getNewmoth(newYearMonth);
		}
	}

	public Integer getOutforworkintJS(String staffint) {
		Uemp uemp = this.getUemp(staffint);
		String jsDate = uemp.getDatepromed();
		String yearMonth = jsDate.substring(0, 6);
		String day = jsDate.substring(6);
		Integer outforworkint = bo.getOutforworkintNow(yearMonth, day);// 应出勤天数

		return outforworkint;
	}

	/**
	 * 得到入职当月出勤天数（不扣除请假）
	 * 
	 * @param staffint
	 * @param period
	 * @return
	 */
	public Integer getOutforworkintRZ(String staffint) {
		Uemp uemp = this.getUemp(staffint);
		String rzDate = uemp.getDateentry();
		String yearMonth = rzDate.substring(0, 6);
		String day = rzDate.substring(6);
		Integer outforworkint = bo.getOutforworkintNow(yearMonth, day);// 应出勤天数

		return outforworkint;
	}

	/**
	 * 得到入职当月出勤天数xxj
	 * 
	 * @param staffint
	 * @param period
	 * @return
	 */
	public Integer getRuforWorad(String staffint) {
		Uemp uemp = this.getUemp(staffint);
		String rzDate = uemp.getDateentry();
		String yearMonth = rzDate.substring(0, 6);
		String day = rzDate.substring(6);
		Integer outforworkint = bo.getforworad(yearMonth, day);// 应出勤天数

		return outforworkint;
	}
	/**
	 * 得到入职当月出勤天数xxj
	 * 
	 * @param staffint
	 * @param period
	 * @return
	 */
	public Integer getRuorad(String staffint) {
		Uemp uemp = this.getUemp(staffint);
		String rzDate = uemp.getDateentry();
		String yearMonth = rzDate.substring(0, 6);
		String day = rzDate.substring(6);
		Integer outforworkint = bo.getOutforworkint(yearMonth);// 应出勤天数

		return outforworkint;
	}
	/**
	 * 得到入职当月出勤天数xxj包含节假日
	 * 
	 * @param staffint
	 * @param period
	 * @return
	 */
	public Integer getruforworad(String staffint) {
		Uemp uemp = this.getUemp(staffint);
		String rzDate = uemp.getDateentry();
		String yearMonth = rzDate.substring(0, 6);
		String day = rzDate.substring(6);
		Integer outforworkint = bo.getforworadtoo(yearMonth, day);// 应出勤天数

		return outforworkint;
	}
    /***
     * xxj 本月离职要加入节假日
     * @param staffint
     * @return
     */
	public Integer getforworad(String staffint) {
		Uemp uemp = this.getUemp(staffint);
		String rzDate = uemp.getDatedimi();
		String yearMonth = rzDate.substring(0, 6);
		String day = rzDate.substring(6);
		Integer outforworkint = bo.getOutForm(yearMonth, day);

		return outforworkint;
	}

	/**
	 * 离职实际出勤天数
	 * 
	 * @return
	 */
	public Integer getOutforworkLZ(String staffint) {
		Uemp uemp = this.getUemp(staffint);
		String lzDate = uemp.getDatedimi();
		String yearMonth = lzDate.substring(0, 6);
		String day = lzDate.substring(6);
		Integer outforworkintLZ = bo.getOutforworkLZ(yearMonth, day);

		return outforworkintLZ;
	}

	/**
	 * 得到实际出勤天数(入职)
	 * 
	 * @return
	 */
	public Double getRealityintNow(String staffint) {
		Uemp uemp = this.getUemp(staffint);
		String rzDate = uemp.getDateentry();
		String yearMonth = rzDate.substring(0, 6);
		String day = rzDate.substring(6);
		Integer outforworkint = bo.getOutforworkintNow(yearMonth, day);// 应出勤天数

		Checktemplet checktemplet = this.getChecktemplet(staffint);
		Double sickday = checktemplet.getSickday();// 病假
		Double personalday = checktemplet.getPersonalday();// 事假
		Double absentday = checktemplet.getAbsentday();// 旷工

		Double realityint = BigdecUitl.sub(
				BigdecUitl.sub(
						outforworkint.doubleValue(),
						BigdecUitl.add(sickday.doubleValue(),
								personalday.doubleValue()).doubleValue())
						.doubleValue(), absentday.doubleValue()).doubleValue();

		return realityint;
	}
	/**
	 * 得到实际出勤天数(上月入职xxj)
	 * 
	 * @return
	 */
	public Double getRealityintNew(String staffint) {
		Uemp uemp = this.getUemp(staffint);
		String rzDate = uemp.getDateentry();
		String yearMonth = rzDate.substring(0, 6);
		String day = rzDate.substring(6);
		double outforworkint = bo.getOutforworkintNow(yearMonth, day); 
 

		return outforworkint;
	}
	/**
	 * 得到实际出勤天数(在职)
	 * 
	 * @return
	 */
	public Double getRealityint(String staffint, String period) {
		Integer outforworkint = this.getOutforworkint(period);// 应出勤天数

		Checktemplet checktemplet = this.getChecktemplet(staffint);
		Double sickday = checktemplet.getSickday();// 病假
		Double personalday = checktemplet.getPersonalday();// 事假
		Double absentday = checktemplet.getAbsentday();// 旷工

		// Integer realityint ;
		// BigdecUitl.sub(outforworkint.doubleValue(),
		// BigdecUitl.add(sickday.doubleValue(),
		// personalday.doubleValue()).doubleValue());
		Double realityint = BigdecUitl.sub(
				BigdecUitl.sub(
						outforworkint.doubleValue(),
						BigdecUitl.add(sickday.doubleValue(),
								personalday.doubleValue()).doubleValue())
						.doubleValue(), absentday.doubleValue()).doubleValue();

		return realityint;
	}

	/**
	 * 得到病假比例
	 * 
	 * @param staffint
	 * @return
	 */
	public Double getSick(Checktemplet checktemplet) {
		String dateentry = this.getUemp(checktemplet.getStaffint()).getDateentry();
		// Date deateentryDate = dateComm.strFormatToDate3(dateentry);
		Double year = dateComm.compareTime(dateentry,checktemplet);
		if (year < 2) {
			return 0.4;

		} else if (year < 4) {
			return 0.3;

		} else if (year < 6) {
			return 0.2;

		} else if (year < 8) {
			return 0.1;

		} else {

			return 0.0;
		}

	}

	/**
	 * 部门总经理、副总经理、见习总助、团队经理以及字母级员(返回true)工仅法定假日加班计加班费，其他时段加班无加班费（标志位)
	 * 
	 * @param staffint
	 * @return
	 */
	public boolean getIdentify(String staffint) {

		return true;
	}

	/**
	 * 获取前6个月的请假天数
	 * 
	 * @return
	 */
	public Double getPersonalday(Checktemplet checktemplet) {
		
		String dateentry = this.getUemp(checktemplet.getStaffint()).getDateentry();
		Integer month = dateComm.calDate(checktemplet,dateentry);
		List<Checktemplet> checktempletList = bo.getChecktemplets(checktemplet.getStaffint());
		Double personalday = new Double(0);
		if (month > 5) {
			// 在职满6个月的请假天数
			for (int i = 0; i < 6; i++) {
				if (checktempletList.get(i).getPersonalday() != null) {
					personalday = personalday
							+ checktempletList.get(i).getPersonalday();
				}
			}
		} else {// 未满6个月
			for (int i = 0; i < checktempletList.size(); i++) {
				if (checktempletList.get(i).getPersonalday() != null) {
					personalday = personalday
							+ checktempletList.get(i).getPersonalday();
				}
			}
		}
		return personalday;
	}

	/**
	 * 得到前6个月应出勤天数
	 * 
	 * @param staffint
	 * @return
	 */
	public Integer getoutwork(Checktemplet checktemplet) {
		//FIXME
		String nowYear = new SimpleDateFormat("yyyy").format(dateComm.getRealDate(checktemplet.getPeriod()));
		String nowMonth = new SimpleDateFormat("MM").format(dateComm.getRealDate(checktemplet.getPeriod()));

		Integer outworkday = new Integer(0);
		String dateentry = this.getUemp(checktemplet.getStaffint()).getDateentry();
		Integer month = dateComm.calDate(checktemplet,dateentry);

		if (month > 5) {
			// 在职满6个月的出勤天数
			if ("01".equals(nowMonth)) {
				String newYear = String.valueOf((Integer.valueOf(nowYear) - 1));
				outworkday = this.getOutforworkint(newYear + "07")
						+ this.getOutforworkint(newYear + "08")
						+ this.getOutforworkint(newYear + "09")
						+ this.getOutforworkint(newYear + "10")
						+ this.getOutforworkint(newYear + "11")
						+ this.getOutforworkint(newYear + "12");

			} else {
				outworkday = this.getOutforworkint(nowYear + "01")
						+ this.getOutforworkint(nowYear + "02")
						+ this.getOutforworkint(nowYear + "03")
						+ this.getOutforworkint(nowYear + "04")
						+ this.getOutforworkint(nowYear + "05")
						+ this.getOutforworkint(nowYear + "06");
			}
		} else {// 未满6个月的出勤天数
			this.getOutforworkintRZ(checktemplet.getStaffint());// 入职当月的应出勤天数
			if ("01".equals(nowMonth)) {
				String newYearMonth = String
						.valueOf((Integer.valueOf(nowYear) - 1)) + "12";
				for (int i = 0; i < month - 1; i++) {
					outworkday = outworkday
							+ this.getOutforworkint(newYearMonth);
					newYearMonth = String
							.valueOf(Integer.valueOf(newYearMonth) - 1);
				}
			} else {
				String newYearMonth = String
						.valueOf((Integer.valueOf(nowYear) - 1)) + "06";
				for (int i = 0; i < month - 1; i++) {
					outworkday = outworkday
							+ this.getOutforworkint(newYearMonth);
					newYearMonth = String
							.valueOf(Integer.valueOf(newYearMonth) - 1);
				}
			}
		}
		return outworkday;
	}

	/***
	 * 正式员工 补发工资（晋升查询调薪后的实际工作日） 根据工号查询出勤表得到员工出勤信息:select count(*) from 表名 where
	 * 日期=？ 业务分析（晋升是查询上个月实际出勤要得到上一个月的） date晋升日期 dates当前日期
	 * 
	 * @return
	 */
	public Integer getfindday(String date, String dates) {
		String mm = dates.substring(4, 6);
		if ("01".equals(mm)) {
			dates = String.valueOf(Integer.valueOf(dates.substring(0, 4)) - 1)
					+ "1231";
		} else {
			// 1.查询日历表得到上个月的select max(日期) from 日历表 where 年月=年月;
			// 2.日期转成string
			CheckDate da = bo.getfinddate(
					String.valueOf(Integer.valueOf(dates.substring(0, 6)) - 1))
					.get(0);
			dates = new SimpleDateFormat("yyyy/MM/dd").format(da.getWorkdate());
		}

		List<CheckDate> dat = bo.getlist(date, dates);
		Integer bel = dat.size();
		return bel;
	}

	/**
	 * 派遣（隔月晋升后实际出勤）晋升日到上上月底的应出勤
	 * 
	 * @param date
	 * @param dates
	 * @return
	 */
	public Integer getfinddayPQ(String date, String dates) {
		String mm = dates.substring(4, 6);
		if ("01".equals(mm)) {
			dates = String.valueOf(Integer.valueOf(dates.substring(0, 4)) - 1)
					+ "1130";

		} else if ("02".equals(mm)) {
			dates = String.valueOf(Integer.valueOf(dates.substring(0, 4)) - 1)
					+ "1231";

		} else {
			// 1.查询日历表得到上个月的select max(日期) from 日历表 where 年月=年月;
			// 2.日期转成string
			CheckDate da = bo.getfinddate(
					String.valueOf(Integer.valueOf(dates.substring(0, 6)) - 2))
					.get(0);
			dates = new SimpleDateFormat("yyyy/MM/dd").format(da.getWorkdate());
		}

		List<CheckDate> dat = bo.getlist(date, dates);
		Integer bel = dat.size();
		return bel;
	}

	/**
	 * 添加员工表时对 基本工资 记录表进行记录
	 * 
	 * @param uemp
	 * @return
	 */
	public String insertJB(Uemp uemp) {
		// 先从jbsalary表中查，无记录时则添加记录
		// 有记录
		// 和uemp表中的moneycount（基本工资）字段比较，有变化则添加一条新记录，并将旧记录进行逻辑删除
		// 若无变化则不操作
		try{
		Jbsalary jbsalary = new Jbsalary();

		jbsalary.setStaffint(uemp.getStaffint());
		jbsalary.setStafftype(uemp.getStafftype());
		jbsalary.setModifyDate(dateComm.getDateNow());

		List<Jbsalary> list = bo.getJbsalaryList(uemp.getStaffint());//已经解密的list
		
		
		if (list.size() > 0) {
			
			String uempMoney = uemp.getMoneycount();      //新增员工的基本工资
			String jbMoney = list.get(0).getStaffmoney(); //数据库中的基本工资
			if (!(jbMoney.equals(uempMoney))) {// 工资有变化
				list.get(0).setDelstaff(1);
				jbsalary.setStaffmoney(uempMoney);

				bo.updateJB(list.get(0));
				bo.insertJB(jbsalary);

				return "Jbsalary was changed";
			} else {
				CryptUtils.encrypt(list.get(0));
				return "jbsalary is no changed";
			}
		} else {
			jbsalary.setStaffmoney(uemp.getMoneycount());
			bo.insertJB(jbsalary);
			return "jbsalary is inserted";
		}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}


	/**
	 *  查询是否封账
	 * @param date
	 * @return
	 */
	public boolean queryLock(String date){
		List<LockState> list =  bo.getLockState(date);
		if(list.size()>0){
			return true ;
		}
		return false;
	}
	/**
	 * 增加封账锁 lock锁
	 * @param date
	 * @return
	 */
	public String insertLock(String date){
		LockState ls = new LockState();
		ls.setLockCheck("1");
		ls.setLockDate(date);
	    return 	bo.insertLock(ls);
	}
	
	/**
	 * 查询 传入日期  是否向前台开放查询
	 * @param date
	 * @return
	 */
	public OpenState getOpenState(String date){
		return bo.getOpenState(date);
	}
	
	/**
	 * 增加或跟新 查询锁 记录
	 * @param openState
	 * @return
	 */
	public String saveOrUpdateOpenState(OpenState openState){
		
		return bo.saveOrUpdateOpenState(openState);
		
	}
	/**
	 * 查询lock锁
	 * @param date
	 * @return
	 */
	public LockState queryLockState(String date){
		return bo.queryLockState(date);
	}

	/**
	 * 查询uemp   修改状态不需要加解密
	 * @return
	 */
	public List<Uemp> getUempList(){
		return bo.getUempList() ;
	}
	
	
	/**
	 * 更新员工（入职、在职、离职状态）   修改状态不需要加解密
	 * @param uemp
	 * @return
	 */
	public String updateUemp(Uemp uemp){
		return bo.updateUemp(uemp);
	}
}
